﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_ToolType_Form : DevExpress.XtraEditors.XtraForm
    {
        //private int m_iNodeID = 0;

        public Tool_System_ToolType_Form()
        {
            InitializeComponent();
        }

        private void Tool_System_ToolType_Form_Load(object sender, EventArgs e)
        {
            _LoadTreeList();
        }

        // 加载工具型号树
        private void _LoadTreeList()
        {
            try
            {
                tl_ToolType.DataSource = ToolDataMgr.GetInstance().GetToolTypeDataTable();
                tl_ToolType.ExpandAll();
            }
            catch
            {
                tl_ToolType.DataSource = null;
                tl_ToolType.Refresh();
            }
        }

        private void bbi_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Tool_System_ToolTypeEdit_Form oForm = new Tool_System_ToolTypeEdit_Form();
            if (oForm.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
            }
        }

        private void bbi_modify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TreeListNode oNode = tl_ToolType.FocusedNode;
            if (oNode != null)
            {
                Tool_System_ToolTypeEdit_Form oForm = new Tool_System_ToolTypeEdit_Form();
                oForm.OToolType.IID = int.Parse(oNode["f_id"].ToString());
                oForm.OToolType.IParentID = int.Parse(oNode["f_parentid"].ToString());
                oForm.OToolType.SName = oNode["f_name"].ToString();
                oForm.OToolType.SPicture = oNode["f_picture"].ToString();
                oForm.OToolType.IRepairCycle = int.Parse(oNode["f_repaircycle"].ToString());

                if (oForm.ShowDialog() == DialogResult.OK)
                {
                    _LoadTreeList();
                }
            }
        }

        private void bbi_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TreeListNode oNode = tl_ToolType.FocusedNode;
            if (oNode != null)
            {
                int iID = int.Parse(oNode["f_id"].ToString());
                string sName = oNode["f_name"].ToString();

                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                String strMsg = "确定要删除" + sName + "的工具型号吗？";
                DialogResult dr = MessageBox.Show(strMsg, "删除", messButton);
                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    if (ToolDataMgr.GetInstance().DelToolType(iID))
                    {
                        _LoadTreeList();
                    }
                }
            } 
        }

        private void tl_ToolType_AfterDragNode(object sender, DevExpress.XtraTreeList.AfterDragNodeEventArgs e)
        {
            TreeListNode oNode = e.Node;
            int iID = int.Parse(oNode["f_id"].ToString());
            int iParentID = 0;


            if (oNode.ParentNode != null)
            {
                iParentID = int.Parse(oNode.ParentNode["f_id"].ToString());
            }

            ToolDataMgr.GetInstance().ModifyToolTypeParent(iID, iParentID);
        }

        private void tl_ToolType_CustomUnboundColumnData(object sender, DevExpress.XtraTreeList.TreeListCustomColumnDataEventArgs e)
        {
            if (e.Column.FieldName == "unPhoto" && e.IsGetData)
            {
                //RefImage是存储图片路径的那一列  
                string sPicture = (string)((DataRowView)e.Row)["f_picture"];
                Image img = Utility.ChangeStringToImage(sPicture);
                e.Value = img;
            }
        }

        private void tl_ToolType_DoubleClick(object sender, EventArgs e)
        {
            bbi_modify.PerformClick();
        }
    }
}